﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using System.Globalization;

namespace Locator.PersonalLocations.Views
{
    public class AddPersonalLocationsPresenter : Presenter<IAddPersonalLocationsView>, IDisposable
    {
        private readonly IPersonalLocationsController controller;

        public AddPersonalLocationsPresenter([CreateNew] IPersonalLocationsController controller)
        {
            this.controller = controller;
        }

        public override void OnViewLoaded()
        {
            this.View.SetProvinces(new List<string>() { "Ontario", "Quebec", "Manitoba" });
        }

        public override void OnViewInitialized()
        {
        }

        public void OnSearchByAddressClicked()
        {
            this.View.Result = String.Empty;
            var location = this.controller.SearchByAddress(this.View.StreetAddress, "CANADA", this.View.Province, this.View.City);

            if (location.Latitude.HasValue && location.Longitude.HasValue)
            {
                this.View.Latitude = location.Latitude;
                this.View.Longitude = location.Longitude;
            }
        }

        public void OnAddPersonalLocationClicked()
        {
            if (this.View.Latitude.HasValue && this.View.Longitude.HasValue && !String.IsNullOrEmpty(this.View.Description))
            {
                try
                {
                    this.controller.AddPersonalLocationForCurrentUser(this.View.Description,
                        (double)this.View.Latitude.Value,
                        (double)this.View.Longitude.Value);
                    this.View.Result = String.Format(CultureInfo.CurrentCulture, "Successfully added personal location {0} with latitude {1} and longitude {2}", this.View.Description, this.View.Latitude, this.View.Longitude);
                    this.View.Reset();
                }
                catch (Exception ex)
                {
                    this.View.Result = String.Format(CultureInfo.CurrentCulture, "Unable to add personal location {0} with latitude {1} and longitude {2}", this.View.Description, this.View.Latitude, this.View.Longitude);
                }
            }
        }

        public void OnSearchByPostalCodeClicked()
        {
            this.View.Result = String.Empty;
            var location = this.controller.SearchByPostalCode(this.View.PostalCode, "CANADA");

            if (location.Latitude.HasValue && location.Longitude.HasValue)
            {
                this.View.Latitude = location.Latitude;
                this.View.Longitude = location.Longitude;
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.controller != null)
                {
                    this.controller.Dispose();
                }
            }
        }

        ~AddPersonalLocationsPresenter()
        {
            this.Dispose(false);
        }
    }
}




